ARG PYVERSION="latest"
FROM python:${PYVERSION}

ARG GROUPID=500
ARG GROUPNAME="develop"
ARG USERID=1000
ARG USERNAME="developer"

ARG PYPATH="/home/${USERNAME}/projects"

# 配置开发环境依赖的工具和设置
RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak && \
    echo " " > /etc/apt/sources.list && \
    echo "deb http://mirrors.aliyun.com/debian buster main" >> /etc/apt/sources.list && \
    echo "deb http://mirrors.aliyun.com/debian buster-updates main" >> /etc/apt/sources.list && \
    apt-get clean && apt-get update && \
    apt-get install -y ripgrep vim sudo && \
    alias py=python3 && \
    pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ && \
    # uncompromising Python code formatter
    pip install black && \
    # flake8 is a python tool that glues together pep8, pyflakes, mccab
    # and third-party plugins to check the style and quality of some python code.
    pip install flake8 && \
    # an awesome autocompletion, static analysis and refactoring library for Python
    pip install jedi && \
    pip install pytest && \
    # a python refactoring library
    pip install rope && \
    # 修改时区
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    apt-get remove -y python2.7 && \
    apt-get autoremove -y && \
    mkdir -p ${PYPATH} && \
    cp /etc/bash.bashrc /home/${USERNAME}/.bashrc && \
    echo "alias p=python3" >> /home/${USERNAME}/.bashrc

# 为开发环境配置用户，避免使用root用户
# 同时为用户分配sudo，方便执行一些root命令
RUN groupadd -r -g ${GROUPID} ${GROUPNAME} && \
    useradd -r -m -g ${GROUPNAME} -u ${USERID} -s /bin/bash ${USERNAME} && \
    # Warning: 这里一定要将$HOME目录的的权限改成$USERNAE否则vscode中无法初始化容器
    chown -R ${USERID}:$GROUPID /home/${USERNAME} && \
    echo ${USERNAME} ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/${USERNAME} && \
    chmod 0440 /etc/sudoers.d/${USERNAME}


WORKDIR /home/${USERNAME}

USER ${USERNAME}
EXPOSE 8080